// pages/xuexi/xuexi.js
const app = getApp()

//单词列表
var word={};
//链接云数据库 初始化单词列表
const db = wx.cloud.database();
var nowtype;
Page({
  data: {
    content: "",
    pron: "",
    definition: "",
    show: false,
    words:[],
    userInfo: {},
  },

  onLoad: function (options) {
    var that = this

    //初始化标题
    var title = options.title
    if (title) {
      wx.setNavigationBarTitle({
        title: title,
      })
    }
    //顺序类型 随机 按字母排序
    var type=options.type;
    if(type){
      nowtype=type;
    }



    //读取全局变量
    this.setData({
      userInfo: app.globalData.userInfo
    })


    //查询数据库总条数
    var total=0;
    wx.cloud.callFunction({
      name:'GetWordList',
      data:{
        dbName: 'wordList',
        filter: {},
        countType:true
      },
      //查询成功后获取数据
      success:res=>{
        total=res.result
        if (res.errMsg == "cloud.callFunction:ok"){
          that.getData(nowtype,total,title)
        }
      }
    })
  },

  /*
   * 点击不认识按钮展示释义 并加入错词本
   */
  show: function () {
    this.setData({
      show: true
    })
    var that=this
    //定义错词库数据
    var data = {
      content: word.content,
      pron:word.pron,
      definition: word.definition
    }
    //把错词添加到数据库
    const wrongWord= db.collection('wrongWord')
    wrongWord.where({
      _openid: app.globalData.userId,
      content:word.content
    }).get({
      success: function (res) {
        //如果错词库不存在本单词
        if(res.data.length==0){
          wrongWord.add({
            //新增的数据
            data: data,
            success: function (res) {
              // console.log(res)
            }

          })
        }
      }
    })
    
  },

  /**
   * 点击下一个
   */
  next: function () {
    var data = {
      title: this.data.title
    }
    this.onLoad(data);
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    app.globalData.index=0;
  },
  /**
   * 获取数据库单词列表数据
   */
  getData:function(nowtype,total,title){
    // var rand = Math.floor(Math.random() * total) + 1;
    var index;
    if(nowtype=='顺序'){
      index = app.globalData.index;
      if (index == total) {
        index = 1;
      }
    }else{
      index = Math.floor(Math.random() * total) + 1;
    }
    
    var size = 1;
    //调用云函数读取数据库数据 自定义分页函数
    wx.cloud.callFunction({
      name: 'GetWordList',
      data: {
        dbName: 'wordList',
        pageIndex: index,
        pageSize: size,
        filter: {},
        orderfiled: 'content',
        ordertype:'asc'
      },
      success: res => {
        //将获取到的数据放在变量里
        var wordlist = res.result.data
        // console.log(wordlist)
        //判断云函数是否返回成功
        if (res.errMsg == "cloud.callFunction:ok") {
          app.globalData.index = index + 1;
          word = wordlist[0];
          //把单词添加到学习库
          if (app.globalData.userId){
            console.log("已登录")
            //定义数据格式
            //连接云数据库 learnhistory
            const learnhistory = db.collection('learnhistory')
            learnhistory.where({
              _openid: app.globalData.userId,
              content: word.content
            }).get({
              success: function (res) {
                //不存在本单词
                if (res.data.length == 0) {
                  console.log(word)
                  learnhistory.add({
                    //新增的数据
                    data: word,
                    success: function (res) {
                      app.getData(1,1,'learnstatus',{id:app.globalData.userId},result=>{
                        // console.log(result.data);
                        //获取学习状态数据
                        var statusdata=result.data[0]
                        //学习单词的数量
                        var total=statusdata.total+1
                        //学习等级
                        var status;
                        //判断当前等级
                        if(total<=10){status='一级'}
                        if (total <= 20 &&total>10) { status = '二级' }
                        if (total <= 30 && total>20) { status = '三级' }
                        if (total <= 40 && total>30) { status = '四级' }
                        if (total <= 50 && total>40) { status = '五级' }
                        if (total <= 60 && total>50) { status = '六级' }
                        if (total <= 70 && total>60) { status = '七级' }
                        if (total <= 80 && total> 70) { status = '八级' }
                        if (total <= 90 && total>80) { status = '九级' }
                        if (total > 90) { status = '十级' }
                        //增加学习的单词数量
                        app.updateData('learnstatus',statusdata._id,{total:total,status:status},ress=>{
                          console.log(ress)
                          console.log("学习状态更新成功")
                        })
                      })
                    }

                  })
                }
              }
            })
          }else{
            console.log("未登录")
          }
          //数据写入前端 页面
          this.setData({
            content: word.content,
            pron: word.pron,
            definition: word.definition,
            show: false,
            title: title,
          });
        }
      }
    })
  }

})